package com.lanou.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.PhotoViewpagerAdapter.PhotoViewPagerAdapterfirst;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entity.Data;
import com.lanou.myapplication.entity.DatarResult;
import com.lanou.myapplication.entity.Pics;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class ViewpageActivity extends FragmentActivity {
    private String id;
    private TextView imgTitle;
    private ViewPager pager;//广告栏部分
    private PhotoViewPagerAdapterfirst pagerAdapter;//广告栏适配器对象
    private RequestQueue queue;//定义一个请求队列


    private Handler handler; //定义一个handler对象,实现广告轮播过程
    private int currentItem = 0;
    private int maxItem = 0;


//    private Runnable r = new Runnable() {
//        @Override
//        public void run() {
//
//            currentItem++;
//
//            maxItem = pagerAdapter.getCount();
//            pager.setCurrentItem(currentItem % maxItem);
//
//            handler.postDelayed(r, 4000);
//        }
//
//    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_viewpage);
        initView();
        getData();
        initData();

    }

    @Nullable

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_pic_viewpage, null);

        return v;
    }


    private void initView() {
        imgTitle = (TextView) findViewById(R.id.imgTitle);

        pager = (ViewPager) findViewById(R.id.picviewPager1);
        //构建适配器对象
        pagerAdapter = new PhotoViewPagerAdapterfirst(getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);//绑定适配器

        //初始化handler对象
        handler = new Handler();


    }

    private void getData() {
        String title = getIntent().getStringExtra("title");
        imgTitle.setText(title);

    }

    private void initData() {
        //初始化请求队列
        queue = Volley.newRequestQueue(this);
        String id = getIntent().getStringExtra("id");
        //获取最新接口消息
        String url = "http://api.sina.cn/sinago/article.json?postt=hdpic_hdpic_toutiao_4&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&id=" + id;
        Log.i("dasdasd", url);


        StringRequest request = new StringRequest(
                url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {

                        Gson gson = new Gson();
                        //按对象解析返回结果
                        DatarResult result = gson.fromJson(
                                response, DatarResult.class);

                        if (result != null) {
                            List<Pics> pics = result.getData().getPics();
                            Log.i("img", "===========" + pics);
                            if (pics != null && pics.size() > 0) {
                                //给广告栏绑定数据
                                pagerAdapter.addData(pics);

                                //设置viewpager最大滑动长度
                                //maxItem=topStories.size();
                                //4秒之后改变viewpager的下标
//                                handler.postDelayed(r, 4000);

                            }

                        }

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {

            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        queue.add(request);//将请求加入到请求队列中

    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (queue != null)
            queue.cancelAll(this);
    }
}